<?php
namespace backend\controllers;


use common\models\SysHotWord;
use common\models\UserHotWord;
use common\service\ConstantService;
use common\service\LogaddService;

class HotWordController extends BaseController
{
    /**
     * 用户热词
     */
    public function actionUserIndex(){
        if($this->isGet() && $this->isAjax()){
            $p = intval( $this->get('page',1) );
            $this->page_size = intval( $this->get('limit',10) );
            $p = ( $p > 0 )? $p : 1;
            $hot_query = UserHotWord::find();
            $hot_query->andWhere(['is_del'=>0]);

            $count = $hot_query->count();

            $offset = ($p - 1) * $this->page_size;

            $lists = $hot_query->orderBy(['update_time' => SORT_DESC,'search_times' => SORT_DESC])
                ->offset( $offset )
                ->limit( $this->page_size )
                ->asArray()
                ->all();
            return $this->renderPageJson($lists,$count,ConstantService::$response_tab_success);
        }

        return $this->render('user-index');

    }

    /**
     * 系统热词
     */
    public function actionSysIndex(){
        if($this->isAjax() && $this->isGet()){
            $p = intval( $this->get('page',1) );
            $this->page_size = intval( $this->get('limit',10) );
            $p = ( $p > 0 )? $p : 1;
            $hot_query = SysHotWord::find();
            $hot_query->andWhere(['is_del'=>0]);
            $count = $hot_query->count();

            $offset = ($p - 1) * $this->page_size;

            $lists = $hot_query->orderBy(['update_time' => SORT_DESC,'search_times' => SORT_DESC])
                ->offset( $offset )
                ->limit( $this->page_size )
                ->asArray()
                ->all();
            return $this->renderPageJson($lists,$count,ConstantService::$response_tab_success);
        }

        return $this->render('sys-index');

    }

    /**
     * 添加热词
     */
    public function actionAddHotWord(){
        if($this->isPost() && $this->isAjax()){
            $word = $this->post('word');
            $type = $this->post('type');
            if(empty($word)){
                return $this->renderJson([],'热词不能为空',ConstantService::$response_fail);
            }
            $row = SysHotWord::findOne(['word'=>$word]);
            if(!empty($row)){
                return $this->renderJson([],'该热词已存在',ConstantService::$response_fail);
            }

            $data['word'] = $word;
            $data['type'] = $type;
            $data['is_del'] = 0;
            $data['search_times'] = 0;
            $data['create_time'] = time();
            $data['update_time'] = time();

            $sys_hot_word = new SysHotWord();
            $sys_hot_word->setAttributes($data);
            if($sys_hot_word->save()){
                $str = $this->admin['username'].'在'.date("Y-m-d H:i:s").'新增热词'.$data['word'];
                LogaddService::logAdd($this->admin['username'],$str);
                return $this->renderJson([],'新增成功',ConstantService::$response_success);
            }
            return $this->renderJson([],'新增失败,请稍后再试',ConstantService::$response_fail);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);

    }

    /**
     * 删除热词
     */
    public function actionDelHotWord(){
        if($this->isAjax() && $this->isPost()){
            $id = $this->post('id');
            $row = SysHotWord::findOne(['id'=>$id]);
            $row->is_del = 1;
            $row->update_time = time();

            if($row->save()){
                $str = $this->admin['username'].'在'.date("Y-m-d H:i:s").'删除热词'.$row->word;
                LogaddService::logAdd($this->admin['username'],$str);
                return $this->renderJson([],'删除成功',ConstantService::$response_success);
            }
            return $this->renderJson([],'删除成功,请稍后再试',ConstantService::$response_fail);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);
    }
}